var jpress = require('../../utils/jpress.js');
var Auth = require('../../utils/auth.js');
var app = getApp();
Page({
    data: {
        text: "Page topic",
        categoriesList: [],
        floatDisplay: "none",
        openId:"",
        userInfo:{}        
    },
    onLoad: function (options) {
        wx.setNavigationBarTitle({
            title: app.globalData.appName +'-专题',
            success: function (res) {
                // success
            }
        });
        Auth.setUserInfoData(this);
        Auth.checkLogin(this);
        this.fetchCategoriesData();
    },
    onShow:function(){            

    },
    //获取分类列表
    fetchCategoriesData: function () {
        var self = this;        
        self.setData({
            categoriesList: []
        });
        jpress.getArticleCategory({
            openId: self.data.openId
        }).then(data => {
            if (data.categories) {
                self.setData({
                    floatDisplay: "block",
                    categoriesList: self.data.categoriesList.concat(data.categories.map(function (item) {
                        if (item.img == null) {
                            item.icon = "../../images/website.png";
                        }else {
                            item.icon = item.img.meta_value
                        }
                        item.subimg = "subscription.png";
                        if(item.subscribe != null && item.subscribe.meta_value == '0'){
                            item.subimg = "subscription-on.png";
                        }
                        return item;
                    })),
                });
            }else {
                console.log(response);
            }
        }).catch(function (response) {
            console.log(response);
        }).finally(function () {

        })
    },
    onShareAppMessage: function () {
        return {
            title: '分享“' + app.globalData.appName + '”的专题栏目.',
            path: 'pages/topic/topic',
            success: function (res) {
                // 转发成功
            },
            fail: function (res) {
                // 转发失败
            }
        }
    },
    postsub: function (e) {
        var self = this;
        var list = self.data.categoriesList;
        if (!self.data.openId) {
            Auth.checkSession(self);
        } else {
            var categoryId = e.currentTarget.dataset.id;
            var openId = self.data.openId;

            jpress.toSubscribe({
                categoryId: categoryId,
                openId: openId
            }).then(data => {
                if (data.status == '0') {
                    setTimeout(function () {
                        wx.showToast({
                            title: '订阅成功',
                            icon: 'success',
                            duration: 900,
                        });
                    }, 200);
                    self.reloadData(categoryId, data.status);
                }else if (data.status == '1') {
                    setTimeout(function () {
                        wx.showToast({
                            title: '取消订阅成功',
                            icon: 'success',
                            duration: 900,
                        });
                    }, 200);
                    self.reloadData(categoryId, data.status);
                }else {
                    setTimeout(function () {
                        wx.showToast({
                            title: '操作失败,请稍后重试',
                            icon: 'success',
                            duration: 900,
                        });
                    }, 900);
                }
            }).catch(function (response) {
                setTimeout(function () {
                    wx.showToast({
                        title: '操作失败,请稍后重试',
                        icon: 'success',
                        duration: 900,
                        success: function () {
                        }
                    });
                }, 900);
                console.log(response);
            })
        }
    },
    // 改变关注状态
    reloadData: function (id, status) {
        var self = this;
        var newCategoriesList = [];
        var subimg = "";
        if (status == "0") {
            subimg = "subscription-on.png"
        }else {
            subimg = "subscription.png"
        }
        var categoriesList = self.data.categoriesList;
        for (var i = 0; i < categoriesList.length; i++) {
            if (categoriesList[i].id == id) {
                categoriesList[i].subimg = subimg;
            }
            newCategoriesList.push(categoriesList[i]);
        }
        if (newCategoriesList.length > 0) {
            self.setData({
                categoriesList: newCategoriesList
            });
        }

    },

    //跳转至某分类下的文章列表
    redictIndex: function (e) {
        //console.log('查看某类别下的文章');  
        var id = e.currentTarget.dataset.id;
        var name = e.currentTarget.dataset.item;
        var url = '../list/list?categoryID=' + id;
        wx.navigateTo({
            url: url
        });
    },

    agreeGetUser: function (e) {        
        let self= this;
        Auth.checkAgreeGetUser(e,app,self);

        setTimeout(function () {
            self.fetchCategoriesData();             
        }, 1000);
    },
    closeLoginPopup() {
        this.setData({ isLoginPopup: false });
    },
    openLoginPopup() {
        this.setData({ isLoginPopup: true });
    },
    getopenId(data) {
        var url = Api.getopenIdUrl();
        var self  = this;
        var postopenIdRequest = wxRequest.postRequest(url, data);
        //获取openId
        postopenIdRequest.then(response => {
            if (response.data.status == '200') {
                //console.log(response.data.openId)
                console.log("openId 获取成功");
                app.globalData.openId = response.data.openId;
                app.globalData.isGetopenId = true;
            }
            else {
                console.log(response);
            }
        })
    },
    confirm: function () {
        this.setData({
            'dialog.hidden': true,
            'dialog.title': '',
            'dialog.content': ''
        })
    }
})